/* eslint-disable */

import * as UBB from './third_party/ubb';
import './third_party/extraTags';

let ubbParser;

function getParser() {
  ubbParser = ubbParser || new UBB({
      defaultColor: '#58666e',            // [option] color of all text element
      linkDefaultColor: '#58666e',        // [option] color of a elment
      flashImage: '/skin/imgs/flash.png', // [option] flash image to show
      tags: {
      }
    });
  return ubbParser;
}

export function ubb2html(value) {
  if (!value) {
    return value;
  }
  const ubbParser = getParser();
  value = value.replace(/(?:\n|\r\n|\r)/g, '').replace(/\[br\]/g, '\n');
  return ubbParser.UBBtoHTML(value);
}


export function html2ubb(value) {
  const ubbParser = getParser();

  const dom = document.createElement('div');
  dom.setAttribute('class', 'invisible');
  dom.innerHTML = value;
  document.body.appendChild(dom);
  let ubb = ubbParser.HTMLtoUBB(dom);
  document.body.removeChild(dom);
  return ubb.replace(/(?:\n|\r\n|\r)/g, '[br]').replace(/>/g, '&gt;').replace(/</g, '&lt;');
}